package com.tm.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.tm.mapper.PermissionMapper;
import com.tm.model.Dept;
import com.tm.model.Permission;
import com.tm.model.Person;
import com.tm.service.PermissionService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author huangp
 * @create 2021-09-12 11:25
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    @Resource
    PermissionMapper permissionMapper;

    @Override
    public List<Map<String, Object>> queryPermission() {
        List<Permission> list = permissionMapper.selectList(null);
        List<Map<String, Object>> childrenList = childrenPermission(0, list);
        return childrenList;
    }

    private List<Map<String, Object>> childrenPermission(Integer pid, List<Permission> list) {
        List<Map<String, Object>> childrenList  =new ArrayList<>();
        list.forEach(a->{
            if(a.getPid()==pid){
                Map<String, Object> map = new HashMap<>();
                map.put("label",a.getName());
                List<Map<String, Object>> children  = childrenPermission(a.getId(),list);
                map.put("id",a.getId());
                if(!children.isEmpty()){
                    map.put("children",children);
                }
                childrenList.add(map);
            }
        });
        return childrenList;
    }

    //新增
    @Override
    public void addPermission(Permission permission) {
        permissionMapper.insert(permission);
    }
    @Override
    public Permission queryPermissionById(Integer id) {
        return permissionMapper.selectById(id);
    }

    @Override
    public void exportFile(HttpServletResponse response) {
        List<Permission> permissions = permissionMapper.selectList(null);
        String fileName = "C:\\Users\\Administrator\\Desktop\\权限信息.xlsx";
        EasyExcel.write(fileName, Permission.class).sheet().doWrite(permissions);
    }
    @Override
    public void uploadFile(MultipartFile dataFile) {
        ArrayList<Permission> permissions = new ArrayList<>();
        try {
            InputStream inputStream = dataFile.getInputStream();
            EasyExcel.read(inputStream, Permission.class, new AnalysisEventListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {
                    permissions.add((Permission) o);
                }
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                    System.out.println("读取成功");
                }
            }).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
        permissions.forEach(permission -> {
            permissionMapper.insert(permission);
        });
    }


}
